/*  This file is part of Extrahexity.
    Copyright (C) 2012 A. Leso, K. Bezdenezhnykh
    Extrahexity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Extrahexity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Extrahexity.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <sstream>

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

#include "resources.h"

int
main(int argc, char *argv[])
{
    std::cout << "Hello, googlecode\n";
    std::cout.flush();
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(1);
    window.setKeyRepeatEnabled(false);
    window.setTitle("Extrahexity");

    sf::Text text("Hello, SFML");
    float angle = 0;
    // Load logo
    sf::Texture logo_tex;
    logo_tex.loadFromFile(RES_GR_LOGO);
    sf::Sprite logo_spr;
    logo_spr.setTexture(logo_tex);

    int fps = 0;
    sf::Clock frame_timer;
    frame_timer.restart();

    bool focus = false;
    while (window.isOpen()) {
        // Process events
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::Resized:
            {
                //TODO: proper resizing
            }   break;
            case sf::Event::KeyPressed:
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
                    text.setRotation(angle=float((int(angle)+10)%360));
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
                    text.setRotation(angle=float((int(angle)-10)%360));
                }
                break;
            case sf::Event::GainedFocus:
                focus = true;
                break;
            case sf::Event::LostFocus:
                focus = false;
            }
        }
        if (focus) {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                text.setRotation(angle=float((int(angle)+1)%360));
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
                text.setRotation(angle=float((int(angle)-1)%360));
            }
        }
        ++fps;
        int time;
        if (( time = frame_timer.getElapsedTime().asMilliseconds()) > 1000) {
            frame_timer.restart();

            std::stringstream ss;

            ss << "Hello, SFML, FPS= ";
            ss << fps;
            ss.flush();
            fps = 0;

            text.setString(ss.str());
        }

        // Redraw
        window.clear();

        window.draw(logo_spr);
        window.draw(text);

        window.display();
    }
    return EXIT_SUCCESS;
}
